package com.hantanger.weblog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hantanger.weblog.model.Post;

/**
 * sitemap.xml生成
 * @author lys
 *
 */
public class SitemapController extends BaseController {
	
	@Override
	public String doGet(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> data = new HashMap<String,Object>();
		
		List<Post> posts = getPostService().getPosts(50, System.currentTimeMillis(), true);
		if(posts == null){
			posts = new ArrayList<Post>();
		}
		data.put("posts", posts);
		data.put("postprevlink", getSpaceUrl() + "/post/prev/");
		
		response.setContentType(XML);
		String rss =  getStringMerger().mergeTemplate(data, "sitemap.ftl");
		try {
			PrintWriter writer = response.getWriter();
			writer.write(rss);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
